/**
 * fileName: MR_YLES_Check_item_barcode_CSV
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(["require", "exports", "N/search", "N/file", "N/record", "N/log", "N/url", "N/runtime", "N/email", "../tools/YLES_Tool", "../tools/YLES_RELATED_RECORDTYPE"], function (require, exports, search, file, record, log, url, runtime, email, YLES_Tool_1, YLES_RELATED_RECORDTYPE_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.summarize = exports.reduce = exports.getInputData = void 0;
    function getInputData(cxt) {
        //1.获取数据
        const logId = (0, YLES_Tool_1.getScriptParams)('custscript_yles_check_item_barcode_log');
        log.debug('logId', logId);
        let rec = record.load({ type: (0, YLES_RELATED_RECORDTYPE_1.CustomRecordType)().IMPORT_ITEM_BARCODE_LOG, id: logId });
        let st = rec.getValue({ fieldId: 'custrecord_yles_status' });
        if (st != '2')
            throw 'wrong status.';
        let csvId = rec.getValue({ fieldId: 'custrecord_yles_csvfile' });
        if (csvId > 0) {
            log.debug('csvId', csvId);
            const csvFile = file.load({ id: csvId });
            //2:check csv
            const validateRes = (0, YLES_Tool_1.validateData)(csvFile, rec.id);
            log.debug('validateRes', validateRes);
            //更新导入日志
            (0, YLES_Tool_1.changeImportLog)(validateRes, logId);
            //发送校验邮件
            sendEmail(validateRes.ckErrorId);
            return null;
        }
        else {
            throw 'there are no deal data.';
        }
    }
    exports.getInputData = getInputData;
    function reduce(context) {
    }
    exports.reduce = reduce;
    function summarize(summary) {
        const logId = (0, YLES_Tool_1.getScriptParams)('custscript_yles_check_item_barcode_log');
        log.debug("执行完成", "end");
        //报错则重置状态
        if (summary.inputSummary.error) {
            record.submitFields({ type: (0, YLES_RELATED_RECORDTYPE_1.CustomRecordType)().IMPORT_ITEM_BARCODE_LOG, id: logId, values: { custrecord_yles_status: '4' } });
        }
    }
    exports.summarize = summarize;
    //发送邮件
    function sendEmail(errId) {
        const logId = (0, YLES_Tool_1.getScriptParams)('custscript_yles_check_item_barcode_log');
        const userId = runtime.getCurrentUser().id;
        const recipientName = search.lookupFields({ type: 'employee', id: userId, columns: ['firstname'] })['firstname'];
        const targetUrl = "https://" +
            url.resolveDomain({
                hostType: url.HostType.APPLICATION
            }) +
            url.resolveScript({
                scriptId: 'customscript_yles_csv_item_barcode_im_sl',
                deploymentId: 'customdeploy_yles_csv_item_barcode_im_sl',
                params: { logId }
            });
        const subject = `【NetSuite Notice】Csv Check Progress is Completed.`;
        const body = `Hi ${recipientName}, 
        Your CSV has checked completed. Please click link below to view results:
        <br/>
        <a href='${targetUrl}'>view csv check results</a>
        <br><br>
        ${errId ? 'There are errors in this CSV. Click the link above for more details' : ''}
    `;
        email.send({
            author: userId,
            recipients: userId,
            subject: subject,
            body: body
        });
        log.debug({ title: '邮件发送成功', details: 'email success' });
    }
});
